# 导包
import time
from appium import webdriver
from appium.options.android import UiAutomator2Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 配置移动端设备信息
des_caps = {
    "platformName": "Android",  # 移动端系统平台
    "platformVersion": "12",  # 平台对应版本
    "deviceName": "mumu",  # 设备名称（可以随便写）
    "appPackage": "com.netease.yanxuan",  # 包名
    "appActivity": ".SplashActivityDefault"  # Activity页面名
}
# 初始化设备配置信息
option = UiAutomator2Options().load_capabilities(des_caps)
# 创建驱动对象
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=option)
time.sleep(1)
# app操作
# 1.进入默认同意协议
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@text="同意"]'))).click()
# 2.点击个人
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@text="个人"]'))).click()
# 3.切换登录方式
# 点击...
WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
    (By.XPATH, '(//android.widget.ImageView[@resource-id="com.netease.yanxuan:id/img_icon"])[2]'))).click()
# 点击"其他登录方式"
WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
    (By.ID, 'com.netease.yanxuan:id/newOtherLoginTypes'))).click()
# 勾选协议
WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
    (By.XPATH, '//android.widget.CheckBox[@resource-id="com.netease.yanxuan:id/check_box"]'))).click()
WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
    (By.XPATH, '(//android.widget.ImageView[@resource-id="com.netease.yanxuan:id/img_icon"])[4]'))).click()
# 4.邮箱及密码登录
# 输入邮箱账号
WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
    (By.ID, 'com.netease.yanxuan:id/account_edit'))).send_keys("19845977089@163.com")
# 输入密码
WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, 'com.netease.yanxuan:id/password_edit'))).send_keys("sy20020927@")
# 点击邮箱账号登录
WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, 'com.netease.yanxuan:id/btn_login_content'))).click()
result = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.XPATH, '//*[@text="用户13015504"]'))).text
print(result)
# 暂停3秒
time.sleep(3)
# 关闭驱动
driver.quit()
